package com.example.com.ddai.taskmanager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.autonavi.minimap.task.IPriorityTask;
import com.autonavi.minimap.task.TaskPriority;
import com.autonavi.minimap.task.engine.BaseProrityTask;
import com.autonavi.minimap.task.engine.TaskManager;
import com.example.com.ddai.others.IMockListener;
import com.example.com.ddai.others.TestRecord;

public class MainActivity extends Activity implements OnClickListener {

	Button bt1, bt2;
	ListView listView;
	Spinner sp1;
	MyAdapter adapter;
	TaskManager tm;
	private ArrayList<TestRecord> data = new ArrayList<TestRecord>();
	HashMap<IMockListener, TestRecord> mapping = new HashMap<IMockListener, TestRecord>();
	private ArrayAdapter<String> spAdapter;
	TaskPriority currentPrority = TaskPriority.UI_MIN;
	// HashMap<TestRecord, MyClickListener> listeners = new HashMap<TestRecord,
	// TestActivity.MyClickListener>();
	OnItemClickListener cacelListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_activity);
		bt1 = (Button) findViewById(R.id.button1);
		bt2 = (Button) findViewById(R.id.button2);
		bt1.setOnClickListener(this);
		bt2.setOnClickListener(this);
		listView = (ListView) findViewById(R.id.listView1);
		adapter = new MyAdapter(this);
		listView.setAdapter(adapter);
		sp1 = (Spinner) findViewById(R.id.spinner1);
		tm = TaskManager.getInstance(0, 4, 4, 2);
		String[] spData = new String[] { "BACK_MIN", "BACK_NORM", "BACK_MAX",
				"UI_MIN", "UI_NORM", "UI_MAX" };
		spAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, spData);
		spAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp1.setAdapter(spAdapter);
		sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				currentPrority = TaskPriority.valueOf(position);
				Log.d("DDAI", "onItemSelected=" + position + " ordinal="
						+ currentPrority.ordinal());
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
		cacelListener = new MyClickListener();
		listView.setOnItemClickListener(cacelListener);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	public void onClick(View v) {

		if (v.getId() == bt1.getId()) {
			test1();
		} else if (v.getId() == bt2.getId()) {
			test2();
		}
	}

	AtomicBoolean ab = new AtomicBoolean(true);

	private void test2() {
		new Thread() {
			public void run() {
				ab.compareAndSet(false, false);
				Log.d("DDAI", "AB="+ab.get());
				ab.compareAndSet(true, false);
				Log.d("DDAI", "AB="+ab.get());
			};
		}.start();
	}

	int taskq = 0;

	private void test1() {
		IMockListener lst = new IMockListener() {

			@Override
			public void onStart() {
				TestRecord tr = mapping.get(this);
				tr.status = "Start";
				runOnUiThread(new Runnable() {
					public void run() {
						adapter.notifyDataSetChanged();
					}
				});
			}

			@Override
			public void onProgress(int i) {
				TestRecord tr = mapping.get(this);
				tr.progress = i;
				runOnUiThread(new Runnable() {
					public void run() {
						adapter.notifyDataSetChanged();
					}
				});
			}

			@Override
			public void onFinish() {
				TestRecord tr = mapping.get(this);
				tr.status = "Done";
				runOnUiThread(new Runnable() {
					public void run() {
						adapter.notifyDataSetChanged();
					}
				});
			}
		};
		TestRecord r = new TestRecord();
		r.title = "TASK" + (++taskq);
		r.progress = 0;
		r.pvalue = currentPrority;
		switch (currentPrority) {
		case BACK_MIN:
			r.prority = "BACK_MIN";
			break;
		case BACK_NORM:
			r.prority = "BACK_NORM";
			break;
		case BACK_MAX:
			r.prority = "BACK_MAX";
			break;
		case UI_MIN:
			r.prority = "UI_MIN";
			break;
		case UI_NORM:
			r.prority = "UI_NORM";
			break;
		case UI_MAX:
			r.prority = "UI_MAX";
			break;
		}
		data.add(r);
		mapping.put(lst, r);
		adapter.notifyDataSetChanged();
		IPriorityTask task = new MyTask(r.title, lst);
		r.taskId = tm.put("C1", task, 0, currentPrority);
	}

	class MyTask extends BaseProrityTask<IMockListener> {
		String title;

		public MyTask(String title, IMockListener listener) {
			super(String.valueOf(System.currentTimeMillis()), listener);
			this.title = title;
		}

		@Override
		public void run() {
			ArrayList<IMockListener> lts = getListeners();
			for (IMockListener l : lts) {
				l.onStart();
			}
			for (int i = 0; i < 101; i++) {
				try {
					Thread.sleep(250);
					lts = getListeners();
					for (IMockListener l : lts) {
						l.onProgress(i);
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			lts = getListeners();
			for (IMockListener l : lts) {
				l.onFinish();
			}
		}
	}

	class MyClickListener implements OnItemClickListener {
		// HashMap<View, TestRecord> records = new HashMap<View, TestRecord>();
		//
		// private volatile Object putLock = new Object();
		//
		// public void putListener(View view, TestRecord r) {
		// synchronized (putLock) {
		// records.put(view, r);
		// }
		// }

		// @Override
		// public void onClick(View v) {
		// TestRecord r = null;
		// synchronized (putLock) {
		// r = records.get(v);
		// }
		// if(r!=null){
		// Log.e("DDAI", "TASK ID = " + r.taskId);
		// tm.cancelTsak(r.taskId);
		// }
		// }

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			TestRecord r = (TestRecord) adapter.getItem(position);
			tm.cancelTsak(r.taskId);
		}
	}

	class MyAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public MyAdapter(Activity activity) {
			mInflater = LayoutInflater.from(activity);
		}

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;

			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.list_item2, null);
				holder = new ViewHolder();
				holder.tv = (TextView) convertView.findViewById(R.id.textView1);
				holder.tvp = (TextView) convertView
						.findViewById(R.id.textView2);
				holder.tvPrority = (TextView) convertView
						.findViewById(R.id.textView3);
				holder.pb = (ProgressBar) convertView
						.findViewById(R.id.progressBar1);
				holder.pb.setMax(100);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			// Bind the data efficiently with the holder.
			TestRecord tr = data.get(position);
			holder.tv.setText(tr.title + " = " + tr.status);
			holder.tvp.setText(tr.progress + "%");
			holder.tvPrority.setText(tr.prority);
			holder.pb.setProgress(tr.progress);
			return convertView;
		}

		private class ViewHolder {
			TextView tv;
			TextView tvp;
			TextView tvPrority;
			ProgressBar pb;
		}

	}

}
